package boot.controller;

import boot.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author: zhiyu
 * @Date:14:15
 * @DESC:
 */
@Controller
@Slf4j
public class UserController {
    @GetMapping("/login.html")
    public String showLogin(){
        return "login";
    }
    @GetMapping("/common")
    public String showCommon(){
        return "common";
    }
    @GetMapping("/index")
    public String showMain(){
        return "index";
    }
    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("token");
        return "login";
    }
    @PostMapping("/login")
    public String handleLogin(String username , String password, Model model,HttpServletRequest request){
        log.info(username,password);
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            model.addAttribute("errMsg","用户名密码不能为空");
            return "login";
        }
        if ("admin".equals(username) && "123".equals(password)){
            HttpSession session = request.getSession();
            session.setAttribute("token","login");
            session.setAttribute("username",username);
            return "redirect:/index";
        }
        model.addAttribute("errMsg","用户名或密码错误");
        return "login";
    }
    @GetMapping("/basic_table")
    public String showTable(Model model){
        //封装 用户集合数据
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("张三","123456","/pic/user1.png"));
        users.add(new User("李四","888888","/pic/user2.png"));
        users.add(new User("王五","555555","/pic/user3.png"));
        users.add(new User("丽丽","999999","/pic/user4.png"));
        //将集合 放置到model域中
        model.addAttribute("users",users);
        return "basic_table";
    }
    @GetMapping("/form_layouts")
    public String showFrom(){
        return "form_layouts";
    }
    //用户头像上传
    @PostMapping("/upload")
    public String upload(
            User user,
            @RequestPart("picture") MultipartFile picture) throws IOException {
        //文件上传
        if(!picture.isEmpty()){
            //得到原始文件名 hello.jpg
            String oldFileName = picture.getOriginalFilename();
            //生成 新的文件名   uuid
            String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));//扩展名
            String newFileName = UUID.randomUUID()+suffix;
            //一键上传
            picture.transferTo(new File("D:\\workspace\\idea\\JKY\\testSpringBoot\\boot3_web_thymeleaf\\src\\main\\resources\\static\\pic\\",newFileName));
            //存储用户的头像路径
            user.setPhoto("/pic/"+newFileName);
            log.info("upload:user:{}",user);
        }
        return "redirect:/index";

    }
    @PutMapping("/uploadFiles")
    public String uploadFiles(
            User user,
            @RequestPart("pictures") List<MultipartFile> pictures) throws IOException {
        //文件上传
        if (!pictures.isEmpty()){
            for(MultipartFile picture:pictures){
                //得到原始文件名 hello.jpg
                String oldFileName = picture.getOriginalFilename();
                //生成 新的文件名   uuid
                String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));//扩展名
                String newFileName = UUID.randomUUID()+suffix;
                //一键上传
                picture.transferTo(new File("D:\\workspace\\idea\\JKY\\testSpringBoot\\boot3_web_thymeleaf\\src\\main\\resources\\static\\pic\\",newFileName));
                //存储用户的头像路径
                user.setPhoto("/pic/"+newFileName);
                log.info("upload:user:{}",user);
            }
        }
        return "redirect:/index";
    }
}
